package com.quartzd.core.config;

import com.quartzd.core.client.QuartzScript;
import com.quartzd.core.job.QuartzJob;
import com.quartzd.util.Func;
import org.quartz.Job;
import org.quartz.JobDataMap;

/**
 * 描述：quartz 配置
 * 该类是定时任务的核心配置了，定时任务行为（增删改查）的关键
 *
 * @author huxuehao
 **/
public class QuartzConfig {
    private String identity;

    /**
     * cron定时策略
     */
    private String cron;

    /**
     * 任务执行参数-可以传入任意类型的数据给执行类
     */
    private final JobDataMap dataMap = new JobDataMap();

    /**
     * 任务执行类-实现了{@code org.quartz.Job}接口的类
     */
    private Class<? extends QuartzJob> jobClass;

    public String getIdentity() {
        checkIdentity();
        return identity;
    }

    public void setIdentity(String identity) {
        if (Func.isEmpty(identity)) {
            throw new RuntimeException("identity 不可为空");
        }
        this.identity = identity;
    }

    public void checkIdentity() {
        if (Func.isEmpty(identity)) {
            throw new RuntimeException("QuartzConfig 中的 identity 未设置");
        }
    }

    public String getCron() {
        return cron;
    }

    public void setCron(String cron) {
        if (QuartzScript.isValidCron(cron)) {
            this.cron = cron;
        } else {
            throw new RuntimeException("cron 不合法");
        }

    }

    public void putDataMap(String key, Object val) {
        this.dataMap.put(key, val);
    }

    public JobDataMap getDataMap() {
        return dataMap;
    }

    public Class<? extends Job> getJobClass() {
        return jobClass;
    }

    public void setJobClass(Class<? extends QuartzJob> jobClass) {
        this.jobClass = jobClass;
    }

    public void checkJobClass() {
        if (jobClass == null) {
            throw new RuntimeException("QuartzConfig 中的 jobClass 未设置");
        }
    }

    public String getJobName() {
        checkIdentity();
        checkJobClass();
        return "QUARTZ_JOB@" + getIdentity();
    }
    public String getJobGroupName() {
        checkIdentity();
        checkJobClass();
        return "QUARTZ_JOB_GROUP@" + getIdentity();
    }

    public String getTriggerName() {
        checkIdentity();
        checkJobClass();
        return "QUARTZ_TRIGGER@" + getIdentity();
    }

    public String getTriggerGroupName() {
        checkIdentity();
        checkJobClass();
        return "QUARTZ_TRIGGER_GROUP@" + getIdentity();
    }
}
